﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.DAL;
using AnyOffice.Components;
using AnyOffice.Model;
using System.Reflection;
using log4net;

namespace AnyOffice
{
    public partial class login_xml : Page
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        
        protected void Page_Load(object sender, EventArgs e)
        {
            string userId = string.Empty;
            string password = string.Empty;
            string url = string.Empty;
            string str4 = "NULL";
            if (Request.QueryString["userid"] != null)
            {
                if (Request.QueryString["userid"] != null)
                {
                    userId = Request.QueryString["userid"].ToString();
                }
                if (Request.QueryString["password"] != null)
                {
                    password = Request.QueryString["password"].ToString();
                }
                password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5");
                if (Request.QueryString["url"] != null)
                {
                    url = Request.QueryString["url"].ToString();
                }
                SqlConnection conn = new SqlConnection(ConfigManager.ConnectionString);
                DataTable table = new UsersDao().Login(conn, userId, password);
                conn.Close();
                if (table.Rows.Count > 0)
                {
                    userId = table.Rows[0]["UserId"].ToString();
                    if (Convert.ToBoolean(table.Rows[0]["Disabled"].ToString()))
                    {
                        SysLog.Log(conn, userId, 16, "");
                        str4 = "DISABLED";
                    }
                    else if (new ConfigManager().LoginKey && Convert.ToBoolean(table.Rows[0]["UsingKey"].ToString()))
                    {
                        SysLog.Log(conn, userId, 17, "需要USB KEY验证");
                        str4 = "FAIL";
                    }
                    else
                    {
                        SysLog.Log(conn, userId, 1, "");
                        FormsAuthentication.SetAuthCookie(userId, true);
                        UserCache.Update(userId);
                        str4 = "SUCESS";
                        if (url != string.Empty)
                        {
                            Response.Redirect(url);
                        }
                    }
                }
                else
                {
                    SysLog.Log(conn, userId, 2, "");
                    str4 = "FAIL";
                }
            }
            Response.Clear();
            Response.ContentType = "text/xml";
            StringBuilder builder = new StringBuilder();
            builder.Append("<?xml version=\"1.0\" encoding=\"gb2312\"?>\n");
            builder.Append("<configuration>\n");
            builder.Append("<version>" + ConfigManager.Version + "</version>\n");
            builder.Append("<userId>" + userId + "</userId>\n");
            builder.Append("<returnCode>" + str4 + "</returnCode>\n");
            builder.Append("</configuration>\n");
            Response.Write(builder.ToString());
        }
        

    }
}
